#!/bin/sh

# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# File to store firmware information derived from crossystem.
BIOS_INFO_FILE="/var/log/bios_info.txt"

# Set up bios information for chrome://system and userfeedback
# Need to do this before user can request in chrome
# moved here to keep out of critical boot path

# Function for showing switch info as reported by crossystem
#
# $1 - crossystem parameter
# $2 - string to return if the value is 0
# $3 - string to return if the value is 1
#
# if $(crossystem $1) reports something else - return 'failure'
swstate () {
  case "$(crossystem $1)" in
    (0) echo $2;;
    (1) echo $3;;
    (*) echo 'failure'
  esac
}

# Function for showing boot reason as reported by crossystem.
bootreason () {
  local reason=$(crossystem 'recovery_reason')

  echo -n "($reason): "
  case "$reason" in
    (0)   echo "$(crossystem 'mainfw_type')";;
    (1)   echo 'Legacy firmware recovery request';;
    (2)   echo 'User pressed recovery button';;
    (3)   echo 'Both RW firmware sections invalid';;
    (4)   echo 'S3 resume failed';;
    (5)   echo 'RO firmware reported TPM error';;
    (6)   echo 'Verified boot shared data initialization error';;
    (7)   echo 'S3Resume() test error';;
    (8)   echo 'LoadFirmwareSetup() test error';;
    (8)   echo 'LoadFirmware() test error';;
    (63)  echo 'Unknown RO firmware error';;
    (65)  echo 'User requested recovery at dev warning screen';;
    (66)  echo 'No valid kernel detected';;
    (67)  echo 'Kernel failed signature check';;
    (68)  echo 'RW firmware reported TPM error';;
    (69)  echo 'Developer RW firmware with the developer switch off';;
    (70)  echo 'RW firmware shared data error';;
    (71)  echo 'LoadKernel() test error';;
    (127) echo 'Unknown RW firmware error';;
    (129) echo 'DM verity failure';;
    (191) echo 'Unknown kernel error';;
    (193) echo 'Recovery mode test from user-mode';;
    (255) echo 'Unknown user mode error';;
  esac
}

if ! mosys -l smbios info bios > ${BIOS_INFO_FILE} 2> /dev/null; then
  # If a sub-command is not available on a platform, mosys will fail with
  # a non-zero exit code (EXIT_FAILURE) and print the help menu. For example,
  # this will happen if a "mosys smbios" sub-command is run on ARM since ARM
  # platforms do not support SMBIOS. If mosys fails, delete the output file to
  # avoid placing non-relevant or confusing output in /var/log.
  echo "version              | $(crossystem fwid)" > ${BIOS_INFO_FILE}
fi

cat <<END >>${BIOS_INFO_FILE}
ro bios version      | $(crossystem ro_fwid)
Boot switch status:
  Recovery button: $(swstate 'recoverysw_boot' 'released' 'pressed')
  Developer mode:  $(swstate 'devsw_boot' 'not enabled' 'selected')
  RO firmware:     $(swstate 'wpsw_boot' 'writeable' 'protected')
Boot reason $(bootreason)
Boot firmware: $(crossystem 'mainfw_act')
Active EC code: $(crossystem 'ecfw_act')
Raw log:
$(crossystem --all)
END

